package com.mz.common.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class R{

    public static Integer OK_CODE = 200;
    public static Integer FAILURE_CODE = -1;
    public static Integer INNER_EXCEPTION_CODE = 500;

    //内码
    private Integer code;
    //内码对应消息
    private String msg;
    //返回数据
    private Object data;
    //额外提示数据
    private Object errorData;
    private Long serverTime;

    public static R deviceOk( Integer code, String msg, Object data ){
        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        r.setServerTime(System.currentTimeMillis());
        return r;
    }

    //有消息形式的返回成功
    public static  R ok( Object data ){

        R r = new R();
        r.setCode(OK_CODE);
        r.setMsg("ok");
        r.setData(data);
        return r;
    }

    //无消息形式的返回成功
    public static R ok(){
        return ok(true);
    }


    //分页形式的消息返回成功
    public static R ok( Long pageIndex, Long pageSize,  Long total,  List data ){

        //处理分页列表数据返回 null 导致数据不规范问题
        if( data == null ){
            data = new ArrayList();
        }
        PageResVo pageResVo = new PageResVo();
        pageResVo.setPageIndex(pageIndex).setPageSize(pageSize).setTotal(total).setList(data);
        return ok(pageResVo);
    }


    //返回自定义编码和内容的信息
    public static R fail( Integer code, String msg ){

        R r = new R();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(null);
        return r;
    }

    //失败返回默认信息
    public static R fail(){
        return  fail(FAILURE_CODE, "");
    }

    public static R fail( Object data ){
        R r = new R();
        r.setCode(400);
        r.setMsg("参数错误");
        r.setData(null);
        r.setErrorData(data);
        return r;
    }
}
